package com.flowable.modules.rest.api;

import com.flowable.modules.rest.service.FlowableService;
import com.flowable.modules.rest.utils.Result;
import org.apache.commons.collections.MultiMap;
import org.flowable.task.api.Task;
import org.flowable.ui.modeler.domain.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/rest/model")
public class ModelApiResource {

    @Resource
    private FlowableService flowableService;

    /**
     * 创建模型并部署
     * @param values
     */
    @PostMapping("/createModel")
    public Result createModel(@RequestBody Map values) {
        try {
            String modelName = values.get("modelName").toString();
            List<Map> steps = (List) values.get("steps");

            Model customModel = flowableService.createCustomModel(modelName, steps);
            String procDefId = flowableService.deploymentProcessDefinitionFromUIModel(customModel);
            return new Result(Result.SUCCESS,procDefId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILED,e.getMessage());
        }
    }

    /**
     * 部署流程实例
     */
    @GetMapping("/{modelKey}/deploymentProcessDefinition")
    public String  deploymentProcessDefinition(@PathVariable String modelKey) {
        List<Model> test = flowableService.getModelByKey(modelKey);
        for (Model model : test) {
            String processDefinition = flowableService.deploymentProcessDefinitionFromUIModel(model);
            return processDefinition;
        }
        return null;
    }

    /**
     * 销毁流程实例
     */
    @GetMapping("/{modelKey}/deploymentDestroy")
    public void deploymentDestroy(@PathVariable String modelKey) {
        List<Model> test = flowableService.getModelByKey(modelKey);
        for (Model model : test) {
            flowableService.deploymentDestroy(model);
        }
    }

}
